package edu.sias.course.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;


import edu.sias.course.domain.dto.TagDTO;
import edu.sias.course.domain.entity.PageResult;
import edu.sias.course.domain.entity.QueryPageBean;
import edu.sias.course.domain.entity.TagPageQueryParams;
import edu.sias.course.domain.pojo.Tag;
import edu.sias.course.domain.vo.TagVO;
import edu.sias.course.mapper.TagMapper;
import edu.sias.course.service.TagService;
import edu.sias.common.utils.DateUtils;
import edu.sias.common.utils.Threads;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Description TagServiceImpl
 * @Author songyu
 * @Date 2023-09-03
 */
@Service
public class TagServiceImpl implements TagService {

    @Resource
    private TagMapper tagMapper;

    /**
     * 学科标签添加方法
     *
     * @param tagDTO
     */
    @Override
    public void add(TagDTO tagDTO) {
        Tag tag = new Tag();
        BeanUtils.copyProperties(tagDTO,tag);
        tag.setCreateDate(DateUtils.dateTime(new Date()));//系统当前时间
        tag.setStatus(0);//启用
        tag.setUserId(Threads.getCurrentId());
        tagMapper.addTag(tag);
    }

    /**
     * 学科标签分页查询
     *
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findListByPage(QueryPageBean<TagPageQueryParams> queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize());

        //调用mapper查询数据
        List<TagVO> tagVOList =  tagMapper.findByCondition(queryPageBean.getQueryParams());
        Page<TagVO> page = (Page<TagVO>) tagVOList;

        //返回数据
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 删除标签
     *
     * @param id
     */
    @Override
    public void deleteTag(Integer id) {
        try {
            tagMapper.deleteTag(id);
        } catch (Exception e) {
            throw new RuntimeException("有关联的面试题，无法删除");
        }
    }
}
